<template>
    <div class="cms-article">
        <div class="cms-article-org">
            <el-select
                    @change="handleSiteChange"
                    style="width: 100%"
                    size = "mini"
                    v-model="catalogForm.siteId"
                    placeholder="请选择站点">
                <el-option
                        v-for="item in this.siteTable"
                        :key="item.ukId"
                        :label="item.name"
                        :value="item.ukId">
                </el-option>
            </el-select>
            <el-table
                    @row-click="handleRowClick"
                    border
                    default-expand-all
                    highlight-current-row
                    :data="catalogTable"
                    row-key="id"
                    height="500"
                    size="mini"
                    style="width: 100%"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        prop="name"
                        label="catalog"
                        width="*">
                </el-table-column>
            </el-table>
        </div>
        <div class="cms-article-table">
            <div style="height: 35px">
                <el-form :model="searchForm" :inline="true" size="mini">
                    <el-form-item label="title">
                        <el-input v-model="searchForm.title"></el-input>
                    </el-form-item>
                    <el-form-item label="author">
                        <el-input v-model="searchForm.author"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="listArticle">Search</el-button>
                        <el-button type="primary" @click="listArticle">Refresh</el-button>
                        <el-button type="primary" @click="handleCreate">New</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table
                    border
                    highlight-current-row
                    row-key="ukId"
                    height="500"
                    size="mini"
                    :data="articleTable"
                    style="width: 100%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="title"
                        label="title"
                        width="*">
                </el-table-column>
                <el-table-column
                        prop="author"
                        label="author"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="status"
                        width="70">
                    <template slot-scope="scope">
                        <el-switch
                                @change="statusChange($event, scope.row.ukId)"
                                :value="scope.row.status != '1'"
                                active-color="#909399"
                                inactive-color="#67c23a">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="sort"
                        label="sort"
                        width="60">
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="210">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handlePreview(scope.row.ukId)">预览
                        </el-button>
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.row.ukId)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog width="1065px" top="10px" :visible.sync="showArticleForm" :close-on-click-modal="false" label-width="80px">
                <el-row style="width: 1024px; height: 700px">
                    <el-col :span="24">
                        <el-form ref="form" :model="articleForm" label-width="80px">
                            <el-form-item label="文章标题">
                                <el-input v-model="articleForm.title" style="width: 100%"></el-input>
                            </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col :span="24">
                        <el-form ref="form" :inline="true" :model="articleForm" label-width="80px">
                            <el-form-item label="文章作者">
                                <el-input v-model="articleForm.author"></el-input>
                            </el-form-item>
                            <el-form-item label="所属栏目">
                                <tree-select
                                        style="width: 200px"
                                        :showCount="true"
                                        :openOnClick="true"
                                        :defaultExpandLevel="3"
                                        noChildrenText="无下级栏目"
                                        v-model="articleForm.catalogId"
                                        :options="catalogTable"
                                        :normalizer="orgTableNormalizer">
                                </tree-select>
                            </el-form-item>
                            <el-form-item label="排序">
                                <el-input v-model="articleForm.sort" type="number" min="0" max="90000"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="handleSave">保存</el-button>
                                <el-button @click="showArticleForm = false">取 消</el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col :span="24">
                        <div ref="editor">
                            <quill-editor
                                    ref="myQuillEditor"
                                    style="height: 500px"
                                    v-model="articleForm.content"
                            />
                        </div>
                    </el-col>
                </el-row>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    import TreeSelect from '@riophae/vue-treeselect'
    import '@riophae/vue-treeselect/dist/vue-treeselect.css'

    import {listSite} from "../../../api/cms/site";
    import {listCatalogTree} from "../../../api/cms/catalog";
    import {
        deleteArticle,
        listArticle,
        saveArticle,
        selectArticleDetails,
        updateArticleStatus
    } from "../../../api/cms/article";
    import axios from 'axios'
    export default {
        name: "Article",
        components: {TreeSelect},
        data() {
            return {
                showArticleForm: false,
                articleForm: {},
                searchForm: {},
                siteTable: [],
                articleTable: [],
                catalogForm: {},
                catalogTable: [],
                orgTableNormalizer(node) {
                    if (!node.children.length) {
                        delete node.children;
                    }
                    return {
                        id: node.ukId,
                        label: node.name
                    }
                }
            }
        },
        mounted() {
        },
        methods: {
            handleCreate() {
                this.articleForm = {
                    catalogId: this.articleForm.catalogId,
                    status: 0,
                    content: ""
                }
                this.showArticleForm = true
            },
            handleEdit(row) {
                this.articleForm = row
                selectArticleDetails(this.articleForm.ukId).then(result => {
                    this.articleForm.content = result.data.articleDetails.content
                    this.showArticleForm = true
                }).catch(error => {
                    console.log(error)
                })
            },
            handleSave() {
                saveArticle(this.articleForm).then(result => {
                    this.listArticle()
                    this.showArticleForm = false
                }).catch(error => {
                    console.log(error)
                })
            },
            handlePreview(ukId) {
                var url = "http://localhost:8901/api/cms/article/" + ukId
                window.open(url, "_blank")
            },
            handleDelete(ukId) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteArticle(ukId).then(result => {
                        this.listArticle()
                        this.showArticleForm = false
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }).catch(error => {
                        console.log(error)
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            statusChange(value, articleId) {
                let status = value ? 0 : 1
                updateArticleStatus(articleId, status).then(result => {
                    this.listArticle()
                }).catch(error => {
                    console.log(error)
                })
            },
            handleContentChange(value, render){
                this.articleForm.content = render
            },
            handleRowClick(row) {
                this.articleForm.catalogId = row.ukId
                this.searchForm.catalogId = row.ukId
                this.listArticle()
            },
            listArticle() {
                listArticle(this.searchForm).then(result => {
                    this.articleTable = result.data.articles.records
                }).catch(error => {
                    console.log(error)
                })
            },
            listSite() {
                listSite({}).then(result => {
                    this.siteTable = result.data.sites
                    this.catalogForm.siteId = this.siteTable[0].ukId
                    this.handleSiteChange(this.catalogForm.siteId)
                }).catch(error => {
                    console.log(error)
                })
            },
            handleSiteChange(siteId) {
                this.catalogForm.siteId = siteId
                this.listCatalogTree()
            },
            listCatalogTree() {
                this.catalogForm.parentId = '0'
                listCatalogTree(this.catalogForm).then(result => {
                    this.catalogTable = result.data.catalogTree
                }).catch(error => {
                    console.log(error)
                })
            }
        },
        created() {
            this.listSite()
        }
    }
</script>

<style>
    .cms-article {
        width: 100%;
        height: 100%;
    }

    .cms-article-org {
        width: 300px;
        height: 100%;
        float: left;
    }

    .cms-article-table {
        padding-left: 5px;
        width: calc(100% - 305px);
        height: calc(100% - 35px);
        float: left;
    }

    /* element ui样式重写 */
    .cms-article > .cms-article-org > .el-table {
        height: calc(100% - 35px) !important;
    }
    .cms-article > .cms-article-table > .el-table {
        height: calc(100% - 5px) !important;
    }

    .cms-article > .cms-article-table > .el-table > .el-table__body-wrapper {
        height: calc(100% - 5px) !important;
    }
</style>
